package jobs;

import java.util.HashSet;
import java.util.Set;

public class BookMangment {
    public static void main(String[] args) {
        Book book1 = new Book("Java编程思想", "Bruce Eckel");
        Book book2 = new Book("设计模式", "GoF");

        Reader reader1 = new Reader("张三", "1001");
        Reader reader2 = new Reader("李四", "1002");

        reader1.borrowBook(book1);
        reader1.borrowBook(book2);
        reader2.borrowBook(book1);

        // 输出book1的所有借阅者
        for (Reader r : book1.getReaders()) {
            System.out.println(r.getName() + " 借阅了 " + book1.getTitle());
        }

        // 输出reader1借阅的所有图书
        for (Book b : reader1.getBorrowedBooks()) {
            System.out.println(reader1.getName() + " 借阅了 " + b.getTitle());
        }
    }
}

class Reader {
    private String name;
    private String id;
    private Set<Book> borrowedBooks = new HashSet<>();

    public Reader(String name, String id) {
        this.name = name;
        this.id = id;
    }

    // 借阅图书
    public void borrowBook(Book book) {
        borrowedBooks.add(book);
        book.addReader(this);
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public Set<Book> getBorrowedBooks() {
        return borrowedBooks;
    }
}

 class Book {
    private String title;
    private String author;
    private Set<Reader> readers = new HashSet<>();

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    // 登记借阅
    public void addReader(Reader reader) {
        readers.add(reader);
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public Set<Reader> getReaders() {
        return readers;
    }
}